package com.Dao.Imp;

import com.Dao.GetAuthorDao;
import com.Utils.C3P0Utils;
import com.pojo.Author;
import com.pojo.AuthorApply;
import com.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GetAuthorDaoImp implements GetAuthorDao {
    @Override
    public Map<String,Object> getAllAuthor(int page, int rows) throws SQLException {
        Map<String,Object> map=new HashMap<>();
        if(page==0){
            page=1;
        }
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        //获取总数
        String sqlCount=" select count(1) from author ";
        Long totalCount = queryRunner.query(sqlCount, new ScalarHandler<Long>());
        int start = (page - 1) * rows; // 起始行索引
        String query = "SELECT * FROM author LIMIT " + start + ", " + rows;
        List<Author> list=queryRunner.query(query,new BeanListHandler<>(Author.class));
        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalCount / rows);
        map.put("list",list);
        map.put("totalPages",totalPages);
       return map;
    }
    @Override
    public Map<String,Object> getAuthorApplyInfo(int page, int rows) throws SQLException {
        Map<String,Object> map=new HashMap<>();
        if(page==0){
            page=1;
        }
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        //获取总数
        String sqlCount=" select count(1) from authorTemporary ";
        Long totalCount = queryRunner.query(sqlCount, new ScalarHandler<Long>());
        int start = (page - 1) * rows; // 起始行索引
        String query = "SELECT * FROM authorTemporary LIMIT " + start + ", " + rows;
        List<AuthorApply> list=queryRunner.query(query,new BeanListHandler<>(AuthorApply.class));
        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalCount / rows);
        map.put("list",list);
        map.put("totalPages",totalPages);
        return map;
    }

}
